require.config({baseUrl: '/js/lib'});
require(['zepto', 'lib'], function ($, lib) {
    var fn = {}, lib = new lib();
    $.extend(fn, {
        init: function () {
            var _t = this;
            _t.getCollectList();
            _t.delCollect();
            $(".ui-nav i").tap(function(){
              location.href = '/cart.html'
            });
        },
        getCollectList: function () {
            lib.ajx(lib.getReq().ser + 'memberCollectGoods/collectionList.action', {
                    pageNo: 1,
                    pageSize: 100,
                    recordCount: 100
                }, function (data) {
                    if (data.infocode == '0') {
                        $('.ui-per-shop').html('');
                        var list = data.info.recordList;
                        var html = '';
                        for (var i in list) {
                            html += '<ul class="ui-goods-info">' +
                                '<li>' +
                                '<div class="ui-g-img"><img src="' + lib.getReq().imgPath + list[i] + '" /></div>' +
                                '<ul class="ui-g-desc">' +
                                '<li>' + list[i].chName + '</li>' +
                                '<li>' +
                                '<i class="ui-g-price">' + list[i].sellPrice + '</i>' +
                                '<i  class="ui-cart"></i>' +
                                '<i class="ui-del" mcg="' + list[i].memberCollectGoodsId + '"></i>' +
                                '</li>' +
                                '</ul>' +
                                '</li>' +
                                '</ul>';
                        }
                        $('.ui-per-shop').append(html);
                    } else if (data.infocode == '2') {
                        location.href = "/login.html?backUrl=" + lib.getBackUrl();
                    } else {
                        alert('服务器开小差了，请稍后再试。')
                    }
                },
                function () {
                    console.log('collect.js => collectionList.action fail!');
                })
        },
        delCollect: function () {
            var _t = this;
            $(document).on('tap', '.ui-del', function () {
                if (confirm('删除此收藏吗？')) {
                    var mcgArr = [$(this).attr('mcg')];
                    lib.ajx(lib.getReq().ser + 'memberCollectGoods/deleteCollections.action', {mcgIds: mcgArr}, function (data) {
                            if (data.infocode == '0') {
                                _t.getCollectList();
                            } else if (data.infocode == '2') {
                                location.href = "/login.html?backUrl=" + lib.getBackUrl();
                            } else {
                                alert('服务器开小差了，请稍后再试。')
                            }
                        },
                        function () {
                            console.log('collect.js => collectionList.action fail!');
                        }
                    )
                }
            })
        }
    });
    fn.init();
});